using System;
using Rapier.Kodachi.Shared;
using System.Net;
using System.Data;
using System.Drawing;
using System.Web;

namespace Rapier.Kodachi.WebSearch
{
    /*
     * This is a generic action. One instance is created for each search engine that is set up.
     */
    public class WebSearchAction : Action
    {
        #region Data Members
        WebSearchEngine engine;
        Bitmap image;
        #endregion

        #region Constructor
        public WebSearchAction(Plugin plugin, WebSearchEngine engine)
            : base(plugin, "Rapier.Kodachi.Weather.WebSearch", "Search with " + engine.Name, "Search the web with the " + engine.Name + " search engine.")
        {
            this.engine = engine;
            this.IndirectItemUse = IndirectItemUse.None;
            image = engine.GetImage(Plugin as WebSearchPlugin);
            GetImage = delegate() { return image; };
        }
        #endregion

        #region Action Members
        public override void Execute(Item[] directItems, Item[] indirectItems)
        {
            foreach(Item directItem in directItems)
            {
                string url = String.Format(engine.QueryFormat, HttpUtility.UrlEncode(directItem.Properties["Text"].ToString()));
                System.Diagnostics.Process.Start(url);
            }
        }

        public override bool IsValidDirect(Item directItem)
        {
            return directItem.Properties.ContainsKey("Text");
        }
        #endregion
    }
}
